package com.liusha.quicksand.web;

import com.liusha.quicksand.po.Type;
import com.liusha.quicksand.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 一世长安一世欢
 * @Date: 2020/10/28
 * @Time: 14:14
 * @Description: No Description
 */

@Controller
@RequestMapping("/admin")
public class TypeController {

    @Autowired
    private TypeService typeService;

    @GetMapping("/type")
    public String list(@PageableDefault(size = 30, sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable, Model model) {

        model.addAttribute("page", typeService.listType(pageable));

        return "admin/type";
    }


    @PostMapping("/type")
    public String post(@Validated Type type, RedirectAttributes attributes, BindingResult result, Model model) {
        /*dddd*/
        model.addAttribute("type", new Type());
        /*dddd*/

        Type type1 = typeService.getTypeByName(type.getName());
        if (type1 != null) {
            result.rejectValue("name", "nameError", "不能添加重复分类！");
        }

        if (result.hasErrors()) {
            return "redirect:/admin/type";
        }

        Type t = typeService.saveType(type);

        if (t == null) {
            attributes.addFlashAttribute("message", "新增失败！");
        } else {
            attributes.addFlashAttribute("message", "新增成功！");
        }

        return "redirect:/admin/type";
    }

    /*修改分类*/
    @PostMapping("/types/{id}")
    public String editPost(@Validated Type type, RedirectAttributes attributes, BindingResult result, @PathVariable Long id) {

        Type type2 = typeService.getTypeByName(type.getName());
        if (type2 != null) {
            System.out.println("fgasdhfgdgfsadhgfjhsdgfhsdgfh");
            result.rejectValue("name", "nameError", "不能添加重复分类！");
        }

        if (result.hasErrors()) {
            System.out.println("fgasdhfgdgfsadhgfjhsdgfhsdgfh");
            return "redirect:/admin/type";
        }

        Type t = typeService.updateType(id, type);

        if (t == null) {
            attributes.addFlashAttribute("message", "更新失败！");
        } else {
            attributes.addFlashAttribute("message", "更新成功！");
        }

        return "redirect:/admin/type";
    }

    @GetMapping("/type/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes attributes) {

        typeService.deleteType(id);
        attributes.addFlashAttribute("message", "删除成功！");

        return "redirect:/admin/type";
    }
}
